#! /home/ecelrc/students/whung1/python3.1/python3.1
import json

from sys import argv
from subprocess import check_call

properties_file_path = "/home/ecelrc/students/whung1/exp/lib/open-chord/config/chord.properties"
command_path = "/home/ecelrc/students/whung1/exp/lib/open-chord/bin/console.sh"
data_path = "/home/ecelrc/students/whung1/exp/lib/open-chord/exp_results/"
output_file_path = "/home/ecelrc/students/whung1/exp/lib/open-chord/eval_script/ori_output"

def write_config(chord_properties, num_primaries, value_size):
     
    print("write config file")
    properties_file = open(properties_file_path, "w+")
    for property in chord_properties:
        properties_file.write(property + "\n") 
    
    properties_file.write("de.uniba.wiai.lspi.chord.service.impl.NodeImpl.MAX_PRIMARY_NODES=")
    properties_file.write(str(num_primaries) + "\n")
    properties_file.write("de.uniba.wiai.lspi.util.console.ConsoleThread.valueSize=")
    properties_file.write(str(value_size) + "\n")




if __name__ == "__main__":
    with open(argv[1]) as config_file:
        config = json.load(config_file)

    iter = config["iter"]
    for i in range(iter):
        for value_size in config["value_size"]:
            for num_primaries in config["num_primaries"]:
                print("num_primaries:" + str(num_primaries) + "iter: " + str(i))
                write_config(config["chord_properties"], num_primaries, value_size)
                check_call([command_path]) 
                new_output_file_path = data_path + "fusion_K" + str(num_primaries) + "_S" + str(value_size) + "_" + str(i)
                check_call(["mv", output_file_path, new_output_file_path])
                print("iter: " + str(i) + " num_primaries: " + str(num_primaries) + " value_size: " + str(value_size) + "\n")
